//
//  FSCFeedBackViewController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/25.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit
import SVProgressHUD

class FSCFeedBackViewController: UIViewController {
    //自定义导航条的初始化
    let navi = FSCLeftViewNavi()
    //发布标题的相关属性
    var textField = UITextField()
    let textFieldHeight = screenHeight * 0.077
    let textFieldY = 64
    //意见反馈的正文内容的相关属性
    var textView = UITextView()
    let textViewH = screenHeight * 0.292
    var placeholderLabel:UILabel?
    var textViewBtn:UIButton?
    var textViewBtnSize = 30
    let btnW = screenWidth * 0.911
    let btnH = screenHeight * 0.078
    let btnColor = UIColor.colorWithHex(hexColor: 0x009ee7)
    let btnText = "提交"
    
    
//MARK:视图生命周期

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.colorWithHex(hexColor: 0xf0f0f0)
        addNavi()
        addTextField()
        addBtn()
        
    }

    //加载导航条
    func addNavi() -> Void {
        navi.addNavigationInleftView(title: "意见反馈", VC: self)
        navi.btnBlock = { [weak self] in
            self?.dismiss(animated: true, completion: nil)
        }
    }
    
    //加载发布标题和正文
    func addTextField() -> Void {
        textField.frame = CGRect(x: 0, y: textFieldY, width: Int(screenWidth), height: textFieldY)
        textField.backgroundColor = UIColor.white
        textField.clearButtonMode = .whileEditing
        textField.placeholder = "请输入标题"
        view.addSubview(textField)
        
        let textViewY = textField.frame.maxY + screenHeight * 0.014
        textView.frame = CGRect(x: 0, y: textViewY, width: screenWidth, height: textViewH)
        textView.delegate = self
        
        textView.keyboardType = UIKeyboardType.default // 键盘类型
        textView.returnKeyType = UIReturnKeyType.done // 键盘右下键 按钮类型
        view.addSubview(textView)
        
        placeholderLabel = UILabel(frame: CGRect(x: 5, y: 5, width: screenWidth, height: 20))
        placeholderLabel?.textAlignment = .left
        placeholderLabel?.font = UIFont.systemFont(ofSize: 16)
        placeholderLabel?.text = "请输入您的宝贵意见,以便我们做的更好"
        placeholderLabel?.numberOfLines = 0
        placeholderLabel?.textColor = UIColor.lightGray
        placeholderLabel?.alpha = 0.7
        textView.addSubview(placeholderLabel!)
        
        textViewBtn = UIButton(type: .custom)
        let btnOrign = Int(screenWidth) - textViewBtnSize - 5
        textViewBtn?.frame = CGRect(x: btnOrign, y: 5, width: textViewBtnSize, height: textViewBtnSize)
        let btnImage = UIImage(named: "清除")
        textViewBtn?.setImage(btnImage, for: .normal)
        textViewBtn?.tag = btnBaseTAG + 20
        textViewBtn?.addTarget(self, action: #selector(btnClicked(btn:)), for: .touchUpInside)
        textViewBtn?.center.x = CGFloat(screenWidth/2)
        textViewBtn?.alpha = 0.0
        textView.addSubview(textViewBtn!)
        
    }
    
    //提交按钮
    func addBtn() -> Void {
        let btnY = textView.frame.maxY + screenHeight * 0.026
        let btn = UIButton(type: .custom)
        btn.layer.cornerRadius = 7
        btn.layer.masksToBounds = true
        btn.frame = CGRect(x: 0, y: btnY, width: btnW, height: btnH)
        btn.center.x = CGFloat(screenWidth/2)
        btn.backgroundColor = btnColor
        btn.setTitle(btnText, for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.addTarget(self, action: #selector(btnClicked(btn:)), for: .touchUpInside)
        let image = UIImage(named: "feedbackBtn_h")
        btn.setBackgroundImage(image, for: .highlighted)
        btn.tag = btnBaseTAG + 10
        view.addSubview(btn)

    }
    
    
    
//MARK:各种btn的点击事件

    func btnClicked(btn:UIButton) -> Void {
        switch btn.tag - btnBaseTAG {
        case 10:
            //提交
            guard textField.text != "" || textView.text != "" else{
                SVProgressHUD.showInfo(withStatus: "标题内容不能为空")
                return
            }
            let urlStr = "ZH-schoolname-S-submitJXOpinion"
            let parm = ["key":userID,"title":textField.text!,"content":textView.text!]
            
            FSCNetworkTools.shared.request(url: urlStr, parameters: parm, finish: { (success, error) in
                guard error == nil else {
                    SVProgressHUD.showInfo(withStatus: "上传失败")
                    return
                }
                SVProgressHUD.showSuccess(withStatus: "上传成功")
                
                
            })
            
        default:
            //textView删除按钮
            textView.text = nil
        }
    
    }
    
}


//MARK:textView 代理

extension FSCFeedBackViewController:UITextViewDelegate {
    
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        if !textView.text.isEmpty {
            textViewBtn?.alpha = 1.0
            placeholderLabel?.isHidden = true
        }
        return true
    }
    
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textViewBtn?.alpha = 0.0
            placeholderLabel?.isHidden = false
        }else {
            textViewBtn?.alpha = 1.0
            placeholderLabel?.isHidden = true
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        
        if textView.text.isEmpty {
            textViewBtn?.alpha = 0.0
            placeholderLabel?.isHidden = false
        }else {
            textViewBtn?.alpha = 1.0
            placeholderLabel?.isHidden = true
        }
        //动态改变删除按钮的位置
        let fontH = textView.contentSize.height
        textViewBtn?.frame.origin.y = fontH
        
        
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        if text == "\n"{ // 输入换行符时收起键盘
            textView.resignFirstResponder() // 收起键盘
            textViewBtn?.alpha = 0.0
        }
        return true
    }
    
}
